"""
下载卷 目的是得到期
"""
import facade
from xjlibrary.our_file_dir import BaseDir

curpath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curpath, -2)
configfile = BaseDir.get_new_path(curpath, "db.ini")
dirPath = BaseDir.get_new_path(TopPath, "download", "intlpress", "download", "issue")
BaseDir.create_dir(dirPath)


class DownVol(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)
        self.baseurl = "https://www.intlpress.com/"
        self.headers = {
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "accept-encoding": "gzip, deflate",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
            "referer": "https://www.intlpress.com/",
            "upgrade-insecure-requests": "1",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
        }

    def select(self):
        sql = "select jid,vol,url,issue from issue where stat=0"
        rows = self.mysqlutils.SelectFromDB(sql)
        for row in rows:
            fullurl = self.baseurl + row[2]
            BoolResult, errString, r = facade.BaseRequest(fullurl,
                                                          headers=self.headers,
                                                          mark="list_item",
                                                          verify=False,
                                                          timeout=(30, 60))
            if BoolResult:
                BaseDir.single_write_file(r.text,
                                          BaseDir.get_new_path(dirPath, row[0] + "_" + row[1] + "_" + row[3] + ".html"))
                sql = "update issue set stat=1 where url = '{}'".format(row[2])

            else:
                sql = "update issue set failcount = failcount + 1 where url = '{}'".format(row[2])

            self.mysqlutils.ExeSqlToDB(sql)


def main():
    down = DownVol()
    down.select()


if __name__ == "__main__":
    main()
